package com.javashow.login.dao.impl;

import java.util.Iterator;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Repository;

import com.javashow.login.dao.ILoginDao;
import com.javashow.pojo.Person;
import com.javashow.pojo.User;

@Repository("loginDao")
public class LoginDaoImpl implements ILoginDao{
	
	@Resource
	private SessionFactory sessionFactory;

	@SuppressWarnings("unchecked")
	@Override
	public boolean login(User user) {
		boolean result = false;
		
		Session session = sessionFactory.getCurrentSession();
		
		Query query = session.createQuery("from User");
		
		List<Person> list = query.list();
		
		if(list != null && list.size() >0) {
			Iterator it = list.iterator();
			
			while(it.hasNext()) {
				User u = (User) it.next();
				if(user.getUsername().equals(u.getUsername())) {
					if(user.getPassword().equals(u.getPassword())) {
						result = true;
					}
				}
			}
		}
		return result;
	}
	
	
}
